import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, setuserid, getuserid, removeUserid, getpath, removeRouterpath, setusername, getusername, removeUsername } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: getusername(),
    h_id: getuserid(),
    path: getpath(),
    avatar: '',
    roles: [],
    buttons: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_ID: (state, h_id) => {
    state.h_id = h_id
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_BUTTON: (state, buttons) => {
    state.buttons = buttons
  },
  SET_PATH: (state, path) => {
    state.path = path
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { account, password } = userInfo
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        console.log(response,"rrr")
        const { result } = response
        commit('SET_TOKEN', result.add_time)
        commit('SET_ID', result.id)
        commit('SET_NAME', result.admin_name)
        setToken(result.add_time)
        setuserid(result.id)
        setusername(result.admin_name)
        resolve()
        localStorage.setItem("avatar",result.avatar)
        localStorage.setItem("adminname",result.account)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('验证失败，请重新登录')
        }
        const { name, avatar, buttons } = data
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_BUTTON', buttons)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // getUserList({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getUserList(state.token).then(response => {
  //       const { result } = response
  //       if (!result) {
  //         return reject('验证失败，请重新登录')
  //       }
  //       const { name, avatar, buttons } = result
  //       commit('SET_NAME', name)
  //       commit('SET_AVATAR', avatar)
  //       commit('SET_BUTTON', buttons)
  //       resolve(result)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },
  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeUserid()
        removeUsername()
        removeRouterpath()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeUserid()
      removeUsername()
      removeRouterpath()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

